import i18n from '@/locales';
// 周期转换
export function formatPeriod(val) {
  if (val == 0) {
    return i18n.tc('userManage.unlimited');
  } else if (val < 60 && val > 0) {
    return i18n.tc('userManage.minutes', val);
  } else if (val >= 60 && val < 1440) {
    let hourRemainder = val % 60;
    let hourQuotient = parseInt(val / 60);
    if (hourRemainder && hourQuotient) {
      return `${i18n.tc('userManage.hours', hourQuotient)} ${i18n.tc(
        'userManage.minutes',
        hourRemainder
      )}`;
    } else if (!hourRemainder && hourQuotient) {
      return i18n.tc('userManage.hours', hourQuotient);
    }
  } else if (val >= 1440) {
    let dayRemainder = val % 1440;
    let dayQuotient = parseInt(val / 1440);
    let hourRemainder = dayRemainder % 60;
    let hourQuotient = parseInt(dayRemainder / 60);
    if (!dayRemainder && dayQuotient && !hourRemainder && !hourQuotient) {
      return i18n.tc('userManage.days', dayQuotient);
    } else if (dayRemainder && dayQuotient && !hourRemainder && hourQuotient) {
      return `${i18n.tc('userManage.days', dayQuotient)} ${i18n.tc(
        'userManage.hours',
        hourQuotient
      )}`;
    } else if (dayRemainder && dayQuotient && hourRemainder && hourQuotient) {
      return `${i18n.tc('userManage.days', dayQuotient)} ${i18n.tc(
        'userManage.hours',
        hourQuotient
      )} ${i18n.tc('userManage.minutes', hourRemainder)}`;
    } else if (dayRemainder && dayQuotient && hourRemainder && !hourQuotient) {
      return `${i18n.tc('userManage.days', dayQuotient)} ${i18n.tc(
        'userManage.minutes',
        hourRemainder
      )}`;
    }
  }
}
// 设备同时在线数量转换
export function formatDevice(val) {
  if (val == 0) {
    return i18n.tc('userManage.minutes', val);
  } else {
    return val;
  }
}
// 上传/下载限速单位转换
export function formatLimit(val) {
  if (val == 0) {
    return i18n.tc('userManage.minutes', val);
  } else if (val < 1024) {
    return `${val}Kbps`;
  } else {
    let remainder = val % 1024;
    return remainder ? `${val}Kbps` : `${val / 1024}Mbps`;
  }
}
// 使用流量单位转换
export function formatUsage(val = 0, type) {
  if (type == 'used') {
    if (val < 1024) {
      return `${val} MB`;
    } else {
      let remainder = val % 1024;
      return remainder ? `${val} MB` : `${val / 1024} GB`;
    }
  } else {
    if (val == 0) {
      return i18n.t('userManage.unlimited');
    } else if (val < 1024) {
      return `${val} MB`;
    } else {
      let remainder = val % 1024;
      return remainder ? `${val} MB` : `${val / 1024} GB`;
    }
  }
}
